package com.sky.task;

import com.alibaba.fastjson.JSON;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Z.lx
 * @Date: 2025/8/2 10:44
 * @Description: 定时对订单处理
 **/

@Component
@Slf4j
public class OrderTask {

    private  OrderMapper orderMapper;
    private  WebSocketServer webSocketServer;

    @Autowired
    public void setOrderMapper(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }
    @Autowired
    public void setWebSocketServer(WebSocketServer webSocketServer) {
        this.webSocketServer = webSocketServer;
    }

    /**
    * @Description 处理支付超时订单
    * @Param []
    * @Date 2025/8/2 11:03
    **/
    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrder(){
        log.info("处理支付超时订单:{}", LocalDateTime.now());

        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);

        List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeOut(Orders.PENDING_PAYMENT, time);

        if (ordersList != null && !ordersList.isEmpty()){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelTime(LocalDateTime.now());
                orders.setCancelReason("支付超时");
                orderMapper.update( orders);
            }
        }
    }

    /**
    * @Description 处理处于待接单订单
    * @Param []
    * @Date 2025/8/2 11:03
    **/
    @Scheduled(cron = "0 0/3 * * * ? ")
    public void processOrder(){
        log.info("处理待接单订单:{}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-3);
        List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeOut(Orders.TO_BE_CONFIRMED, time);
        if (ordersList != null && !ordersList.isEmpty()){
            for (Orders orders : ordersList) {
                Map<String, Object> map = new HashMap<>();
                map.put("type", 2);
                map.put("orderId", orders.getId());
                map.put("content", "订单提醒：订单号" + orders.getNumber() + "已超时，请及时处理");
                String jsonString = JSON.toJSONString(map);
                webSocketServer.sendToAllClient(jsonString);
            }
        }
    }
    /**
     * @Description 处理处于待派送订单
     * @Param []
     * @Date 2025/8/2 11:03
     **/
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void deliveryOrder(){
        log.info("处理待派送订单:{}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeOut(Orders.CONFIRMED, time);
        if (ordersList != null && !ordersList.isEmpty()){
            for (Orders orders : ordersList) {
                Map<String, Object> map = new HashMap<>();
                map.put("type", 2);
                map.put("orderId", orders.getId());
                map.put("content", "订单待派送提醒：订单号" + orders.getNumber() + "已超时，请及时处理");
                String jsonString = JSON.toJSONString(map);
                webSocketServer.sendToAllClient(jsonString);
            }
        }
    }


    /**
    * @Description 处理处于一直派送订单
    * @Param []
    * @Date 2025/8/2 11:03
    **/
    @Scheduled(cron = "0 0 1 * * ?")
    public void processTimeoutDeliveryOrder(){
        log.info("处理一直在配送的订单:{}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);

        List<Orders> ordersList = orderMapper.getByStatusAndOrderTimeOut(Orders.DELIVERY_IN_PROGRESS, time);

        if (ordersList != null && !ordersList.isEmpty()){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update( orders);
            }
        }
    }
}
